﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace RealEstateCenter
{
    public partial class EditCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                customerID.Text = Request.QueryString["id"];
                Session["edit_customer_id"] = Request.QueryString["id"];
                string connectionString;
                SqlConnection sqlconnect;
                connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|RealEstateCenter.mdf;Integrated Security=True;User Instance=True";
                sqlconnect = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand("select * from Customer where customer_id='" + Request.QueryString["id"] + "'", sqlconnect);
                sqlconnect.Open();
                SqlDataReader dtrReader = cmd.ExecuteReader();
                if (dtrReader.Read())
                {
                    customerName.Text = dtrReader["customer_name"].ToString();
                    customerPhone.Text = dtrReader["customer_tel_no"].ToString();
                    customerEmail.Text = dtrReader["customer_mail"].ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string edit_cust_id = Session["edit_customer_id"].ToString();
            string connectionString;
            SqlConnection sqlconnect;
            connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|RealEstateCenter.mdf;Integrated Security=True;User Instance=True";
            sqlconnect = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("update Customer set customer_name='"+customerName.Text+"', customer_tel_no='"+customerPhone.Text+"', customer_mail='"+customerEmail.Text+"' where customer_id="+Session["edit_customer_id"].ToString(), sqlconnect);
            sqlconnect.Open();
            int kontrol = cmd.ExecuteNonQuery();

            if (kontrol == 1)
            {
                Response.Redirect("ListCustomers.aspx");
            }
        }
    }
}